package com.musifind.activities;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.musifind.adapters.MatchedUserListAdapter;
import com.musifind.models.MatchedUsersItems;
import com.musifind.utils.ImageUtils;
import com.musifind.utils.SessionManager;
import com.musifind.wsclient.objects.Matching;
import com.musifind.wsclient.service.MatchingService;

public class MatchedUsersActivity extends Activity {

	private ListView lstMatchedUsers;
	MatchedUserListAdapter matchAdapter;
	ArrayList<MatchedUsersItems> matchArray = new ArrayList<MatchedUsersItems>();
	private boolean isOnline;
	private ProgressDialog progressDialog = null;
	private SessionManager session;
	private TextView txtNoMatches;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_matched_users);
		session = new SessionManager(getApplicationContext());

		// activity starts as push old activity to left
		overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);

		txtNoMatches = (TextView) findViewById(R.id.txtNoMatches);

		progressDialog = new ProgressDialog(MatchedUsersActivity.this);
		matchAdapter = new MatchedUserListAdapter(MatchedUsersActivity.this, R.layout.matched_users_list_row,
				matchArray);
		lstMatchedUsers = (ListView) findViewById(R.id.lstMatchedUsers);
		lstMatchedUsers.setAdapter(matchAdapter);

		lstMatchedUsers.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			}
		});
		new SenkronizeToAsyncTask().execute();

	}

	public class SenkronizeToAsyncTask extends AsyncTask<String, Void, String> {
		@Override
		protected void onPreExecute() {
			progressDialog = new ProgressDialog(MatchedUsersActivity.this);
			progressDialog.setTitle("Yükleniyor");
			progressDialog.setMessage("Lütfen bekleyiniz...");
			progressDialog.setCancelable(false);
			progressDialog.show();
		}

		@Override
		protected String doInBackground(String... params) {
			runOnUiThread(new Runnable() {
				public void run() {
					MatchingService matchingService = new MatchingService();
					List<Matching> matchings = matchingService.getMatchingsByUserId(session.getUserId());
					for (Matching matching : matchings) {
						matchArray.add(new MatchedUsersItems(matching.getId(), ImageUtils.getBitmap(matching.getTo()
								.getImage()), matching.getTo().getAd() + " " + matching.getTo().getSoyad()));
					}
					if (lstMatchedUsers.getAdapter().getCount() > 0) {
						txtNoMatches.setVisibility(View.INVISIBLE);
					}

				}
			});
			return null;
		}

		@Override
		protected void onPostExecute(String s) {
			progressDialog.dismiss();
		}
	}
}